import os
import time
from tkinter import messagebox


fileUrl = input("请输入干翻的文件夹绝对路径")
oldName = input("请输入你觉得欠揍文件的名字")
newName = input("请输入你希望他被干翻后的样子")
isALL = input("是否干翻整个文件夹内所有子级文件夹（Y/N）") or "n"
start = input("请输入'我TM确认执行'程序就开始动手了")


# ↓ ------------------ 干活总该他娘的有个进度条吧 --------------------
def process_bar(percent, start_str='', end_str='', total_length=0, filename="", Path=""):
    bar = ''.join(['===='] * int(percent * total_length)) + ''
    bar = '\r' + start_str + bar.ljust(total_length) + ' {:0>4.1f}%|'.format(
        percent * 100) + end_str + ' (' + filename + ")"
    print(bar, end='', flush=True)
    if float(percent * 100) >= 100:
        print("\n文件夹" + Path + "中的所有目标已倒地不起")


# ↓ ---------------- 找到命令程序他妈的要去找的文件 ------------------
def screenTheWords(url, oldname, newName, isALL):
    files = os.listdir(url)
    # ↓ -------- 列出他妈的当前目录下所有的文件 --------
    # print(type(files))
    for i, filename in enumerate(files):
        time.sleep(.1)
        end_str = '100%'
        # percentage = (i+1)/4*100
        # print(percentage+'%')
        process_bar((i + 1) / len(files), start_str='', end_str=end_str, total_length=10, filename=filename, Path=url)
        portion = os.path.splitext(filename)
        theTextSubscript = portion[0].find(oldname)  # ← 筛选出需要他妈的使用的文字
        theFileName_new = ""
        if theTextSubscript != -1:
            textHEAD = portion[0][0:theTextSubscript]  # ← 未筛他妈的选到的前方文字
            latterName = portion[0][theTextSubscript + len(oldname):len(portion[0])]  # ← 未筛他妈的选到的后方文字
            theFileName_new = textHEAD + newName + latterName + portion[1]
            os.renames(url + '\\' + filename, url + '\\' + theFileName_new)

        # ↓ ---------------------- 干翻全场的递归函数 ------------------
        if isALL == "Y" or isALL == "y":
            ThenextPath = theFileName_new if len(theFileName_new) > 0 else filename
            if not os.path.isfile(url + '\\' + ThenextPath):
                nextPath = url + '\\' + ThenextPath
                screenTheWords(nextPath, oldname, newName, isALL)
    # messagebox.showinfo("Fuck ALL", "我他妈干翻了你想要干翻的文件夹" + url)


# ↓ ---------------- 他都他妈的确认了 那我们干就他妈的就完了 --------------------
if (start == '我TM确认执行'):
    screenTheWords(fileUrl, oldName, newName, isALL)


# ================================= 2021.06.18 启凡 =============================================